# -*- coding: utf-8 -*-
"""
@Time：YourTime
@Author  : YourName
@Desc    : 
"""
import json

from kubernetes import client, config
import requests
from kubernetes import client, config
# from requests.packages.urllib3.exceptions import InsecureRequestWarning
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def get_csrf_token():
    url = "https://k8s.dxbim.com/api/v1/csrftoken/login"
    data = {
        "token": "yaj7BcacCb0pHst3ZC0LBOSW1f8:1721615974815"
    }
    resp = requests.request(method='GET', url=url, params=data)
    return resp.json()


def get_k8s_api():
    token2 = "eyJhbGciOiJSUzI1NiIsImtpZCI6Ilh1QkZSYVhKbUhlMDk0N0NhUVpLdXhsZk8xcG1YaDFwMHY4bHJqR25aczgifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWN0eHM1Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJjYjJiNjE1Yi1jMzY0LTQ4NGQtOGRlMy1kZDcwYzE3ZTk3Y2EiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.fKC2rMl0SZa9ULT7cWmCFGs3FacMf_QJPEHPcDLIdMd-0wQFW_q3JYAY_lAbQg3kEUl0hT9nrvkW92OzC6w9aFu29gaLZaTVOF7eUUwSx0u7iaAHjhF6xA_hYBKO0H5JLg0eq53KVgqRTeXHeGZiIk0VBGMFwok8UGfqaswccburZedxFCTCUV-UHckhGf31d7GgipVsW4PeIZlKYWrkVNkGeRqk8rSR8cQA0uhWteOxBU25O7kXQs2oiz71yXAvUuEDpw__pCrqHNDMx4Ej4lTP3qfPnabJYvwimELzt3yBoSgwHxqKdX-EoTUP1KBvDV9et_JQ0mJb_w5QOVxkjw"
    url = "https://k8s.dxbim.com/api/v1/login"
    headers = {
        "Content-Type": "application/json",
        "X-Csrf-Token": get_csrf_token()["token"]
    }
    data = {
        "token": token2
    }
    resp = requests.request(method='POST', url=url, json=data, headers=headers)
    return resp.json()


def get_service_config(configmap_list, is_web, is_rpc):
    headers = {
        "Content-Type": "application/json",
        "Jwetoken": get_k8s_api()['jweToken']
    }
    if is_rpc:
        for rpc in configmap_list:
            print("-->当前rpc服务为：", rpc)
            url = "https://k8s.dxbim.com/api/v1/configmap/xbase-test/{}".format(rpc)
            resp = requests.request(method='GET', url=url, headers=headers).json()['data']['testing.yaml']
            for fs_config_info in resp.split('\n'):
                if fs_config_info.endswith('fs服务host') or fs_config_info.endswith('fs服务port'):
                    print(fs_config_info)
    elif is_web:
        for web in configmap_list:
            print("-->当前前端服务为：", web)
            url = "https://k8s.dxbim.com/api/v1/configmap/xbase-test/{}".format(web)
            resp = requests.request(method='GET', url=url, headers=headers).json()['data']['frontConfig.json']
            sdk_config_info = json.loads(resp)
            for key, value in sdk_config_info.items():
                if key == "dxyp" or key == "modelManagement" or key == "sceneManagement":
                    for sdk in value:
                        if sdk == "jssdkServer":
                            print(value[sdk]['dx3dJs'], value[sdk]['staticHost'], value[sdk]['dxSceneEditorJs'])


if __name__ == "__main__":
    rpc_config_fs = ['paas-asset-rpc-configmap', 'paas-component-rpc-configmap', 'paas-modelfile-rpc-configmap']
    get_service_config(rpc_config_fs, False, True)
    print("--------我是分割线--------")
    web_config_sdk = ['paas-web-front-configmap', 'paas-model-management-web-front-configmap',
                      'paas-scene-web-front-configmap']
    get_service_config(web_config_sdk, True, False)
